﻿/*Copyright (C) 2010-2012 RAAFCOM B.V. All rights reserved.

This file is part of Velsto and may be used under the terms of:
The GNU General Public License version 3 or higher, as published by
the Free Software Foundation.

This file is provided AS IS with NO WARRANTY OF ANY KIND, INCLUDING THE
WARRANTY OF DESIGN, MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE.*/
/*
* Application  : Velsto
* Class        : SingleresultsDataTable
* 04/06/2010-  : M.A.van Ravesteijn (Raafcom)
*   02/12/2011   Initial version v1.0 
* 29/08/2012   : v1.2 added Layername 
* Class for Data table for Single simulation results
*/ 
using System;
using System.Collections.Generic;
using System.Collections;
using System.Linq;
using System.Text;
using System.Data;
using System.IO;
using System.Globalization;

namespace Velsto
{
    public class SingleresultsDataTable : DataTable
    {
        public SingleresultsDataTable()
        {
            DataColumn c1 = new DataColumn();
            c1.DataType = System.Type.GetType("System.String");
            c1.ColumnName = "LayerName";
            c1.ReadOnly = false;
            this.Columns.Add(c1);
            DataColumn c2 = new DataColumn();
            c2.DataType = System.Type.GetType("System.Double");
            c2.ColumnName = "Twt";
            c2.ReadOnly = false;
            this.Columns.Add(c2);
            DataColumn c3 = new DataColumn();
            c3.DataType = System.Type.GetType("System.Double");
            c3.ColumnName = "\u03c3Twt";
            c3.ReadOnly = false;
            this.Columns.Add(c3);
            DataColumn c4 = new DataColumn();
            c4.DataType = System.Type.GetType("System.String");
            c4.ColumnName = "Vel.model";
            c4.ReadOnly = false;
            this.Columns.Add(c4);
            DataColumn c5 = new DataColumn();
            c5.DataType = System.Type.GetType("System.Double");
            c5.ColumnName = "V";
            c5.ReadOnly = false;
            this.Columns.Add(c5);
            DataColumn c6 = new DataColumn();
            c6.DataType = System.Type.GetType("System.Double");
            c6.ColumnName = "\u03c3V";
            c6.ReadOnly = false;
            this.Columns.Add(c6);
            DataColumn c7 = new DataColumn();
            c7.DataType = System.Type.GetType("System.Double");
            c7.ColumnName = "K";
            c7.ReadOnly = false;
            this.Columns.Add(c7);
            DataColumn c8 = new DataColumn();
            c8.DataType = System.Type.GetType("System.Int32");
            c8.ColumnName = "DepVel";
            c8.ReadOnly = false;
            this.Columns.Add(c8);
            DataColumn c9 = new DataColumn();
            c9.DataType = System.Type.GetType("System.Int32");
            c9.ColumnName = "DepV0k";
            c9.ReadOnly = false;
            this.Columns.Add(c9);
            DataColumn c10 = new DataColumn();
            c10.DataType = System.Type.GetType("System.Double");
            c10.ColumnName = "Avz";
            c10.ReadOnly = false;
            this.Columns.Add(c10);
            DataColumn c11 = new DataColumn();
            c11.DataType = System.Type.GetType("System.Double");
            c11.ColumnName = "Sdz";
            c11.ReadOnly = false;
            this.Columns.Add(c11);
        }

        // Method to parse with both decimal separators
        public double parseDoubleString(string sd)
        {
            var currentCulture = System.Globalization.CultureInfo.InstalledUICulture;
            var numberFormat = (System.Globalization.NumberFormatInfo)
            currentCulture.NumberFormat.Clone();
            numberFormat.NumberDecimalSeparator = ".";
            double number;
            bool succeeded = double.TryParse(sd, System.Globalization.NumberStyles.Any, numberFormat, out number);
            if (succeeded)
            {
                return number;
            }
            else
            {
                numberFormat.NumberDecimalSeparator = ",";
                bool succeeded2 = double.TryParse(sd, System.Globalization.NumberStyles.Any, numberFormat, out number);
                if (succeeded2)
                {
                    return number;
                }
                else
                {
                    return -999.25;
                }
            }
        }

        public SimulationEntity fillSingleresultsDataTable(SimulationEntity se, ArrayList alVtypes)
        {
            try
            {
                int i2 = 0;
                int[] iv0 = new int[10];
                int[] ivvndev = new int[10];
                int[] itwtdev = new int[10];
                for (int i = 0; i < 10; i++)
                {
                    if (((String)se.AlInputFileTWTdev[i]).Length == 0)
                    {
                        itwtdev[i] = -1;
                    }
                    else
                    {
                        itwtdev[i] = i2;
                        i2++;
                    }
                }
                for (int i = 0; i < 10; i++)
                {
                    if (((String)se.AlInputFileV0[i]).Length == 0)
                    {
                        iv0[i] = -1;
                    }
                    else
                    {
                        iv0[i] = i2;
                        i2++;
                    }
                }
                for (int i = 0; i < 10; i++)
                {
                    if (((String)se.AlInputFileVVNdev[i]).Length == 0)
                    {
                        ivvndev[i] = -1;
                    }
                    else
                    {
                        ivvndev[i] = i2;
                        i2++;
                    }
                }
                if (!File.Exists(se.JoinSingleFile))
                {
                    se.ErrorMessage = "Cannot find Input Single Point Simulation";
                    return se;
                }
                if (!File.Exists(se.SingleAvzFile))
                {
                    se.ErrorMessage = "Cannot find Results Single Point Simulation";
                    return se;
                }
                if (!File.Exists(se.SingleSdzFile))
                {
                    se.ErrorMessage = "Cannot find Results Single Point Simulation";
                    return se;
                }
                TextReader readJoinSingle = new StreamReader(se.JoinSingleFile);
                TextReader readSingleAvz = new StreamReader(se.SingleAvzFile);
                TextReader readSingleSdz = new StreamReader(se.SingleSdzFile);
                char[] delimiterChars = { ' ', '\t' };
                string line3 = readJoinSingle.ReadLine();
                line3.Replace(',', '.');
                readJoinSingle.Close();
                string[] wordsJoin = line3.Split(delimiterChars,
                                               StringSplitOptions.RemoveEmptyEntries);
                
                string line = readSingleAvz.ReadLine();
                readSingleAvz.Close();
                string[] wordsAvz = line.Split(delimiterChars,
                                               StringSplitOptions.RemoveEmptyEntries);
                string line2 = readSingleSdz.ReadLine();
                readSingleSdz.Close();
                string[] wordsSdz = line2.Split(delimiterChars,
                                                StringSplitOptions.RemoveEmptyEntries);
                DataRow row;
                for (int i = 0; i < se.NrLayers; i++)
                {
                    row = this.NewRow();
                    if (!((String)se.AlLayername[i]).Equals(""))
                    {
                        row[0] = (String)se.AlLayername[i];
                    }
                    else
                    {
                        row[0] = i+1;
                    }
                    row[1] = parseDoubleString(wordsJoin[i + 2]);
                    if (itwtdev[i] == -1)
                    {
                        row[2] = ((LayerparamsEntity)(se.AlLayerParams[i])).TwtDev;
                    }
                    else
                    {
                        row[2] = parseDoubleString(wordsJoin[2 + se.NrLayers + itwtdev[i]]);
                    }
                    row[3] = alVtypes[((LayerparamsEntity)(se.AlLayerParams[i])).V0vintOption].ToString();
                    if (iv0[i] == -1)
                    {
                        row[4] = ((LayerparamsEntity)(se.AlLayerParams[i])).V0vint;
                    }
                    else
                    {
                        row[4] = parseDoubleString(wordsJoin[2 + se.NrLayers + iv0[i]]);
                    }
                    if (ivvndev[i] == -1)
                    {
                        row[5] = ((LayerparamsEntity)(se.AlLayerParams[i])).V0vintDev;
                    }
                    else
                    {
                        row[5] = parseDoubleString(wordsJoin[2 + se.NrLayers + ivvndev[i]]);
                    }
                    row[6] = ((LayerparamsEntity)(se.AlLayerParams[i])).K;
                    row[7] = ((LayerparamsEntity)(se.AlLayerParams[i])).DepVel;
                    row[8] = ((LayerparamsEntity)(se.AlLayerParams[i])).DepV0K;
                    row[9] = parseDoubleString(wordsAvz[i + 2]);
                    row[10] = parseDoubleString(wordsSdz[i + 2]);
                    this.Rows.Add(row);
                }
                return se;
            }
            catch (Exception ex)
            {
                se.ErrorMessage = ex.ToString();
                return se;
            }
        }

    }
}
